package com.example.myshixun;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.myshixun.ui.DetailActivity;
import com.example.myshixun.ui.FoodListAdapter;
import com.example.myshixun.ui.FoodListBean;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

public class MyShixun3 extends AppCompatActivity implements View.OnClickListener {
    private ListView IvFood;
    Button buttonMy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_shixun3);
        initView();
        gatDate();
    }

    private void gatDate() {
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        String url = "http://192.168.124.126:8080/18/Menu/all";
        JSONObject jsonObject = new JSONObject();

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Gson gson = new Gson();
                        FoodListBean foodListBean = gson.fromJson(jsonObject.toString(),FoodListBean.class);
                        if (foodListBean.isSuccess()){
                            final List list = foodListBean.getList();
                            FoodListAdapter adapter = new FoodListAdapter(list,getApplicationContext());
                            IvFood.setAdapter(adapter);
                            IvFood.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    Intent intent = new Intent(MyShixun3.this, DetailActivity.class);
                                    intent.putExtra("id",position+1);
                                    startActivity(intent);
                                }
                            });
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                });
                requestQueue.add(jsonObjectRequest);
    }

    private void initView() {
        IvFood = findViewById(R.id.liebiao);
        //buttonMy = findViewById(R.id.my);
        //buttonMy.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
////            switch (v.getId()){
////                    case R.id.my;
////                    Intent intent = new Intent(this,Myshixun4)
//            }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}